//
//  AddContentViewController.swift
//  willdo
//
//  Created by zz on 4/2/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit
import SVProgressHUD

class AddContentViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
    let imgView = UIImageView()
    let nameLabel = UITextField()
    
    let positionImg = UIImageView()
    let positionView = UITextField()
    
    let telImg = UIImageView()
    let telView = UITextField()
    
    let noteImg = UIImageView()
    let noteView = UITextField()
    
    var viewY:CGFloat = 64.0
    override func viewDidLoad() {
        super.viewDidLoad()
        SVProgressHUD.setMinimumDismissTimeInterval(1.0)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "创建", style: .plain, target: self, action: #selector(addContact))
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        SetupView()
    }
    
    @objc func getImgFromPhoto(){
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        present(picker, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let image : UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            return
        }
        picker.dismiss(animated: true) {
            self.imgView.image = image
        }
    }

    
    @objc func addContact(){
        let name = nameLabel.text!
        let pos = positionView.text!
        let tel = telView.text!
        let note = noteView.text!
        let avatar = UIImagePNGRepresentation(imgView.image!)
        if name.isEmpty || pos.isEmpty || tel.isEmpty || note.isEmpty{
            SVProgressHUD.showInfo(withStatus: "还有项目未填写")
            return
        }
        let contact = ExternalMemberModel(id:0, name: name, position: pos, tel: tel, note: note, avatar: avatar!)
        DBTool.shareDBTool().addContactDB(data: contact)
        SVProgressHUD.showInfo(withStatus: "添加成功")
        navigationController?.popViewController(animated: true)
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        let textY = textField.convert(view.bounds, to: nil).origin.y
        if textY > UIScreen.main.bounds.height/3.0{
            UIView.animate(withDuration: 0.4, animations: {
                self.view.frame.origin.y -= textY - UIScreen.main.bounds.height/3.0
            })
        }
        firstRespondView = textField
        return true
    }
    
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        if firstRespondView == textField{
            UIView.animate(withDuration: 0.4, animations: {
                self.view.frame.origin.y = self.viewY
            })
        }
        return true
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        firstRespondView.resignFirstResponder()
        return true
    }
    
    func SetupView() {
        view.backgroundColor = BACKGROUND_GRAY
        let topView = UIView()
        topView.backgroundColor = UIColor.white
        view.addSubview(topView)
        topView.backgroundColor = UIColor.white
        topView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(180)
        }
        topView.addSubview(imgView)
        topView.addSubview(nameLabel)
        imgView.layer.cornerRadius = 45
        imgView.clipsToBounds = true
        imgView.isUserInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(getImgFromPhoto))
        imgView.addGestureRecognizer(tapGesture)
        imgView.image = UIImage(named: "head portrait")
        imgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(25)
            make.centerX.equalToSuperview()
            make.height.width.equalTo(90)
        }
        
        let centerStyle = NSMutableParagraphStyle()
        centerStyle.alignment = .center
        nameLabel.attributedPlaceholder = NSMutableAttributedString(string: "姓名", attributes: [NSAttributedStringKey.paragraphStyle : centerStyle])
//        nameLabel.placeholder = "姓名"
        nameLabel.delegate = self
        nameLabel.textAlignment = .center
        nameLabel.font = UIFont(name: "PingFang SC", size: 18)
        nameLabel.textColor = UIColor(hexString: "#496069")
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(imgView.snp.bottom).offset(21)
            make.left.right.equalToSuperview()
            make.centerX.equalTo(imgView)
        }
        
        let posView = getCell(img: positionImg, label: positionView)
        view.addSubview(posView)
        positionImg.image = UIImage(named: "work_picture")
        positionView.placeholder = "工作岗位"
        posView.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom).offset(20)
            make.left.right.equalToSuperview()
            make.height.equalTo(52)
        }
        let sepLine0 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        view.addSubview(sepLine0)
        sepLine0.backgroundColor = BACKGROUND_GRAY
        sepLine0.snp.makeConstraints { (make) in
            make.width.left.equalToSuperview()
            make.top.equalTo(posView.snp.bottom)
            make.height.equalTo(1)
        }
        
        let phoneView = getCell(img: telImg, label: telView)
        view.addSubview(phoneView)
        telImg.image = UIImage(named: "contact_picture")
        telView.placeholder = "联系方式"
        phoneView.snp.makeConstraints { (make) in
            make.top.equalTo(sepLine0.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(posView)
        }
        
        let sepLine1 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        view.addSubview(sepLine1)
        sepLine1.backgroundColor = BACKGROUND_GRAY
        sepLine1.snp.makeConstraints { (make) in
            make.width.left.equalToSuperview()
            make.top.equalTo(phoneView.snp.bottom)
            make.height.equalTo(1)
        }
        let note = getCell(img: noteImg, label: noteView)
        view.addSubview(note)
        noteImg.image = UIImage(named: "remark_picture")
        noteView.placeholder = "备注"
        note.snp.makeConstraints { (make) in
            make.top.equalTo(sepLine1.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(noteView.snp.bottom).offset(12)
        }
    }
    
    private func getCell(img:UIView, label:UITextField) -> UIView{
        let cellView = UIView()
        view.addSubview(cellView)
        cellView.backgroundColor = UIColor.white
        cellView.addSubview(img)
        cellView.addSubview(label)
        img.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(17)
            make.left.equalToSuperview().offset(27)
            make.height.width.equalTo(18)
        }
        label.sizeToFit()
        label.delegate = self
        label.font = UIFont(name: "PingFang SC", size: 15)
        label.textColor = UIColor(hexString: "#496069")
        label.snp.makeConstraints { (make) in
            make.top.equalTo(img)
            make.left.equalTo(img.snp.right).offset(17)
            make.right.equalToSuperview()
            
        }
        return cellView
    }
    
    
    
}
